<?php
if( !defined('IN_INDEX') )
{
	posterror("ACCESS DENIED", "This page is hidden.");
	exit;
}
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
if($USER->getRank() >= 5)
{
}
else
{
	header('Location: index.php?action=login');
	exit;
}

//Select the values from the settings table
$sql = "SELECT settings_trainings_to_show, settings_comments_to_show, settings_drift_message, settings_news_per_page, settings_news_to_show, settings_version, settings_invitecode FROM settings";
if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
		$row = $db->sql_fetchrow($result);
		$nr_of_trainings_to_show = $row['settings_trainings_to_show'];
		$nr_of_news_to_show = $row['settings_news_to_show'];
		$nr_of_comments_to_show = $row['settings_comments_to_show'];
		$driftmessage = $row['settings_drift_message'];
		$news_per_page = $row['settings_news_per_page'];
		$version = $row['settings_version'];
		$invitecode = $row['settings_invitecode'];

//Update the settings
if(isset($_POST['admin_settings_submit']))
{
	$settings_checked = 0;
	if(isset($_POST['nr_of_trainings_to_show']) && !empty($_POST['nr_of_trainings_to_show']) && isset($_POST['nr_of_comments_to_show']) && !empty($_POST['nr_of_comments_to_show']))
	{
		$nroftrainings = $_POST['nr_of_trainings_to_show'];
		$nrofcomments = $_POST['nr_of_comments_to_show'];
		$nrofnews = $_POST['nr_of_news_to_show'];
		$message = $_POST['drift_message'];
		$newsperpage = $_POST['news_per_page'];
		$version = $_POST['version'];
		$invitecode = $_POST['invitecode'];

	//Update the DB
	$sql = "UPDATE settings SET settings_trainings_to_show=". $nroftrainings .", settings_comments_to_show=". $nrofcomments .", settings_drift_message='". $message ."', settings_news_per_page=". $newsperpage .", settings_news_to_show=". $nrofnews .", settings_version='". $version ."', settings_invitecode='". $invitecode ."' ";
	if(!$result = $db->sql_query($sql))
		{
			printError("SQL ERROR", mysql_error($sql));
		}
	
	
	echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><meta http-equiv=\"refresh\"content=\"3;url=index.php?action=profile&sub=edit_admin\">";
		echo "<h2>" . ucfirst(translate('settings_saved')) . "</h2>\n";
		echo "<br/>" . translate('redirectedin'). " <a href=\"index.php?action=profile&sub=edit_admin\">" . translate('clickhere') . "</a>.</center>
		<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
		exit;
	}
}

$array_posts = array(1, 3, 5, 7, 10, 15, 20, 30, 50, 100);
include('inc/functions_blog.php');
?>

<div id='postpath'>
	<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
    &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
    &gt; <a href="index.php?action=profile&id=<?php echo $USER->getId(); ?>"><?php echo ucf($USER->getFirstname()) . " " . ucf($USER->getLastname()); ?></a>
	&gt; <a href="index.php?action=profile&sub=edit" title='<?php echo translate('edit'); ?>'><?php echo translate('edit'); ?></a> 
	&gt; <a href="index.php?action=profile&sub=edit_admin" title='<?php echo translate('aminsettings'); ?>'><?php echo translate('adminsettings'); ?></a> 
</div>
<br />

<h2><?php echo translate('adminsettings'); ?></h2>
<br />
<form action="index.php?action=profile&sub=edit_admin" method="post" id="admin_settings" name="admin_settings">

    <h2><?php echo translate('frontpage_settings'); ?></h2>
    <br />
    
    	<table border="1" width="100%" cellspacing="0" cellpadding="3">        
        		<tr>
                	<td width="37%" valign="top">
    					<img src="img/info.png" /> <b><?php echo translate('drift_message'); ?></b> 
                		<div class="set_subtext"><?php echo translate('showsamessageonfrontpage'); ?></div>
                	</td>
                	<td width="63%">
						<input type="text" name="drift_message" size="78" value="<?php echo strip_tags($driftmessage); ?>"  />
                	</td>
                </tr>
        		<tr>
                	<td>
    					<img src="img/tr_show.png" /> <b><?php echo translate('number_of_trainings'); ?></b> 
                		<div class="set_subtext"><?php echo translate('choosehowmanytrainingstoshow'); ?></div>
                	</td>
                	<td>
						<input type="text" name="nr_of_trainings_to_show" size="20" value="<?php echo $nr_of_trainings_to_show; ?>"  />
                	</td>
                </tr>
                <tr>
                	<td>
    					<img src="img/newspaper.png" /> <b><?php echo translate('number_of_news'); ?></b> 
                		<div class="set_subtext"><?php echo translate('choosehowmanynewstoshow'); ?></div>
                	</td>
                	<td>
						<input type="text" name="nr_of_news_to_show" size="20" value="<?php echo $nr_of_news_to_show; ?>"  />
                	</td>
                </tr>
               <tr>
                	<td>
  						<img src="img/comments.png" /> <b><?php echo translate('number_of_comments'); ?></b> 
                		<div class="set_subtext"><?php echo translate('choosehowmanycommentstoshow'); ?></div>
                	</td>
                	<td>
						<input type="text" name="nr_of_comments_to_show" size="20" value="<?php echo $nr_of_comments_to_show; ?>"  />
                	</td>
                </tr>
                
	</table> 
    
  <br />
  <h2><?php echo translate('news_settings'); ?></h2>
  <br />
  
  <table border="1" width="100%" cellspacing="0" cellpadding="3">
        		<tr>
                	<td width="37%" valign="top">
    					<img src="img/newspaper_add.png" /> <b><?php echo translate('new_article'); ?></b> 
                		<div class="set_subtext"><?php echo translate('clicktoaddanewarticle'); ?></div>
                	</td>
                	<td width="63%">
						<a href="index.php?action=news&sub=new"><?php echo translate('clickherenow'); ?></a>.
                	</td>
                </tr>
                
               <tr>
                	<td>
  						<img src="img/ppp_edit.png" /> <b><?php echo translate('number_of_news_pr_page'); ?></b> 
                		<div class="set_subtext"><?php echo translate('choosehowmanynewsperpagetoshow'); ?></div>
                	</td>
                	<td>
						<select name="news_per_page">
				<?php
					foreach($array_posts as $key => $val)
					{
						if($val == $news_per_page)
						{
							echo "<option value=\"" . $val . "\" selected=\"selected\">" . $val . "</option>\n";
						}
						else
						{
							echo "<option value=\"" . $val . "\">" . $val . "</option>\n";
						}
					}
				?>
					<option value="0"><?php echo translate('allposts'); ?></option>
				</select>
                	</td>
                </tr>
                
	</table> 
    
    <br />
    <h2><?php echo translate('diverse_settings'); ?></h2>
  <br />
  
  <table border="1" width="100%" cellspacing="0" cellpadding="3">
  				<tr>
                	<td width="37%" valign="top">
    					<img src="img/author.png" /> <b><?php echo translate('chatsettings'); ?></b> 
                		<div class="set_subtext"><?php echo translate('clicktoopenchatsettings'); ?></div>
                	</td>
                	<td width="63%">
						<a href="chat/admin/"><?php echo translate('clickherenow'); ?></a>.
                	</td>
                </tr>
        		<tr>
                	<td width="37%" valign="top">
    					<img src="img/information.png" /> <b><?php echo translate('version'); ?></b> 
                		<div class="set_subtext"><?php echo translate('writewichversionthesiteis'); ?></div>
                	</td>
                	<td width="63%">
						<input type="text" name="version" size="20" value="<?php echo $version; ?>"  />
                	</td>
                </tr>
                <tr>
                	<td width="37%" valign="top">
    					<img src="img/information.png" /> <b><?php echo translate('invitecodeheader'); ?></b> 
                		<div class="set_subtext"><?php echo translate('invitecode'); ?></div>
                	</td>
                	<td width="63%">
						<input type="text" name="invitecode" size="20" value="<?php echo $invitecode; ?>"  />
                	</td>
                </tr>
				<tr>
                	<td width="37%" valign="top">
    					<img src="img/newspaper_add.png" /> <b><?php echo translate('newawardheader'); ?></b> 
                		<div class="set_subtext"><?php echo translate('newaward'); ?></div>
                	</td>
                	<td width="63%">
						<a href="index.php?action=profile&sub=new_award"><?php echo translate('clickherenow'); ?></a>
                	</td>
                </tr>
	</table> 
    
    <br /><br />
  
  <input style="font-weight:bold;" type="submit" value=" <?php echo translate('save'); ?> " name="admin_settings_submit" />   
</form>